<?php

class userActions extends sfActions
{
	public function executeRegister()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			return sfView::SUCCESS; //Display the form
		}
		else
		{
			//Handle the form submission
			$user = new sfGuardUser();
			$user->setUsername($this->getRequestParameter('loginname'));
			$user->setPassword($this->getRequestParameter('password'));
			$user->setIsActive(false);
			$profile = $user->getProfile();
			$profile->setFirstName($this->getRequestParameter('firstname'));
			$profile->setLastName($this->getRequestParameter('lastname'));
			$profile->setEmailAddress($this->getRequestParameter('email'));
			$profile->save();

			$this->setFlash('key', $user->getSalt());
			$email = $this->sendEmail('mail','sendActivation');

			return 'NewUser';
		}
	}

	public function handleErrorRegister()
	{
		return sfView::SUCCESS;
	}

	public function executeActivate()
	{
		$login = $this->getRequestParameter('login');
		$key = $this->getRequestParameter('key');
		$user = sfGuardUserPeer::retrieveByUsername($login, false);

		if ($user)
		{
			$salt = $user->getSalt();
			if ($salt == $key)
			{
				$user->setIsActive(true);
				$user->save();

				return sfView::SUCCESS; //User activated
			}
			else
			{
				return 'ErrorKey'; //Wrong key
			}
		}
		else
		{
			$user = sfGuardUserPeer::retrieveByUsername($login);

			if ($user)
			{
				return 'UserActivated'; //User already activated
			}
			else
			{
				return 'ErrorUser'; //No such user found
			}
		}
	}

	public function executeView()
	{
		$uname = $this->getRequestParameter('username');
		$this->enableedit = false;

		if ($uname == 'me')
		{
			if ($this->getUser()->isAuthenticated())
			{
				$this->userx = $this->getUser()->getGuardUser();
				$this->enableedit = true;
			}
			else
			{
				$this->forward404();
			}
		}
		else
		{
			$this->userx = sfGuardUserPeer::retrieveByUsername($uname);
			$this->forward404Unless($this->userx);

			if ($this->getUser()->isAuthenticated())
			{
				$this->enableedit = $this->getUser()->getGuardUser()->getUsername() == $uname;
			}
		}

		return sfView::SUCCESS;
	}

	public function executeEdit()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			return sfView::SUCCESS; //Display the form
		}
		else
		{
			//Save profile
			$profile = $this->getUser()->getProfile();
			$profile->setFirstName($this->getRequestParameter('firstname'));
			$profile->setLastName($this->getRequestParameter('lastname'));
			$profile->save();
			$this->redirect('@viewuser');
		}
	}

	public function handleErrorEdit()
	{
		return sfView::SUCCESS;
	}

	public function executePassword()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			return sfView::SUCCESS; //Display the form
		}
		else
		{
			//Save password
			$user = $this->getUser()->getGuardUser();
			$user->setPassword($this->getRequestParameter('password'));
			$user->save();
			$this->redirect('@viewuser');
		}
	}

	public function handleErrorPassword()
	{
		return sfView::SUCCESS;
	}

	public function executeEmail()
	{
		if ($this->getRequest()->getMethod() != sfRequest::POST)
		{
			return sfView::SUCCESS; //Display the form
		}
		else
		{
			//Save Email
			$profile = $this->getUser()->getGuardUser()->getProfile();
			$profile->setEmailAddress($this->getRequestParameter('email'));
			$profile->save();
			$this->redirect('@viewuser');
		}
	}

	public function handleErrorEmail()
	{
		return sfView::SUCCESS;
	}
}